#include "preprocesamiento.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

void preprocesar(Mat src, Mat &dst,int cannyThresh ){
	Mat aux = src.clone();
	dst = src.clone();
	Mat aux2;
	equalizeHist( aux, aux );
	if (cannyThresh < 20) cannyThresh = 20;

	blur(aux, dst, Size(3, 3));
	Canny(dst, dst, cannyThresh/2, cannyThresh*2.5 , 3);
	dilate(dst, dst, Mat(), Point(-1,-1));
	//erode(dst,dst,Mat(), Point(-1,-1));
};
void preprocesar(Mat src, Mat &dst,int cannyThresh1,int cannyThresh2){
	Mat aux = src.clone();
	dst = src.clone();
	Mat aux2;

	blur(aux, dst, Size(3, 3));
	Canny(dst, dst, cannyThresh1, cannyThresh2 , 3);
	dilate(dst, dst, Mat(), Point(-1,-1));
	//erode(dst,dst,Mat(), Point(-1,-1));
};
void preprocesarPlaca(Mat src, Mat &dst ){
	Mat aux = src.clone();
	Mat channel[3];
	split(aux, channel);
	Mat aux2 = Mat::zeros(src.size(), CV_8UC1);
	
	for (int i = 0; i < src.cols*src.rows; i++){
		int r = channel[2].data[i], g = channel[1].data[i], b = channel[0].data[i];
		if ( g > r && g > b){
			channel[2].data[i] = (r*2 > 255 ? 255 : r*2);
			channel[1].data[i] = (g*2 > 255 ? 255 : g*2);
			channel[0].data[i] = (b*2 > 255 ? 255 : b*2);
		}
	}
    merge(channel,3,aux2);

	cvtColor(aux2, dst , CV_BGR2GRAY);
	//imshow("preproc", aux2);
	threshold(dst, dst, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
};
void preprocesarPlaca(Mat src, Mat &dst,int thresh ){
	//cvtColor(src, dst , CV_BGR2GRAY);
	dst = src.clone();
	threshold(dst, dst, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
};